<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput
					v-model="username"
					type="text"
					maxlength="11"
					placeholder="用户名/电话"
					:focus="isFocus"
				></wInput>
				<wInput
					v-model="password"
					type="password"
					maxlength="11"
					placeholder="密码"
				></wInput>
			</view>
			<wButton 
				class="wbutton"
				text="登 录"
				:rotate="isRotate" 
				@click="login"
			></wButton>
			
			<!-- 其他登录 -->
			<view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-weibo" @tap="login_weibo"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-github" @tap="login_github"></view>
				</view>
			</view>
			
			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	import {mapGetters} from 'vuex'
	export default {
		computed: {
			...mapGetters({
				hasLogin: 'hasLogin',
				userInfo: 'userInfo'
			})
		},
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				phoneData:'', //用户/电话
				passData:'', //密码
				isRotate: false, //是否加载旋转
				isFocus: true ,// 是否聚焦
				username:'123456',
				password:'123456'
			};
		},
		components:{
			wInput,
			wButton,
		},
		mounted() {
			if(this.hasLogin){
				//处理过期
				uni.reLaunch({url:"../../pages/order/examine"})
			}
		},
		methods: {
			async login(){
				if (this.username.length == "") {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '用户名不能为空'
				    });
				    return;
				}
				if (this.password.length < 5) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '密码不正确'
				    });
				    return;
				}
				
				let res=await this.$api('user-center','login',{username:this.username,password:this.password})
				if(res.code===0){
					uni.setStorageSync('uniIdToken', res.token)
					uni.setStorageSync('uni_id_token_expired', res.tokenExpired)
					await this.$store.commit('user/login',res.userInfo)
					uni.reLaunch({
						url:"../../pages/order/examine",
						fail: (err) => {
							console.log(err)
						}
					})
				}else{
					uni.showToast({
						icon:'none',
						title:res.msg
					})
				}
			},
			gotoRegistration: function () {
				uni.navigateTo({url: 'registration'});
			},
			gotoForgetPassword: function () {
				uni.navigateTo({url: 'forget-password'});
			},
			login_weixin() {
				//微信登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_weibo() {
				//微博登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_github() {
				//github登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
